//
//  WWYReadDetailViewController.m
//  Pianke
//
//  Created by Daydream on 15/12/28.
//  Copyright © 2015年 dada. All rights reserved.
//

#import "WWYReadDetailViewController.h"
#import "WWYNewestTableViewController.h"
#import "WWYReadListModel.h"
#import "WWYReadDetailModel.h"

@interface WWYReadDetailViewController ()<UIScrollViewDelegate>


//分区控制器
@property (nonatomic, strong) UISegmentedControl *segment;

//下划线
@property (nonatomic, strong) UIView *line;

//容器控制器,子视图
@property (nonatomic, strong) WWYNewestTableViewController *hotestTC;

@property (nonatomic, strong) NSMutableArray *hotestArray;

@property (nonatomic, strong) WWYNewestTableViewController *newestTC;

@property (nonatomic, strong) NSMutableArray *newestArray;

@property (nonatomic, strong) UIScrollView *scroll;

//菊花
@property (nonatomic, strong) MBProgressHUD *hub;


@end

@implementation WWYReadDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //分区
    self.segment = [[UISegmentedControl alloc] initWithItems:@[@"最新", @"最热"]];
    //去掉颜色,现在整个segment都看不见
    self.segment.tintColor = [UIColor clearColor];
    self.segment.backgroundColor = [UIColor whiteColor];
    //设置文字属性
    //选中
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName:[UIColor colorWithRed:0.06 green:0.52 blue:0.98 alpha:1]};
    [self.segment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    //没选中
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:15],NSForegroundColorAttributeName: [UIColor darkGrayColor]};
    //默认选中
    self.segment.selectedSegmentIndex = 0;
    [self.segment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    //添加监听
    [self.segment addTarget:self action:@selector(changeSubView:) forControlEvents:UIControlEventValueChanged];
    //
    [self.view addSubview:self.segment];
    [self.segment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(@66);
        make.left.right.mas_equalTo(@0);
        make.height.mas_equalTo(@40);
    }];
    
    //按钮下划线
    self.line = [[UIView alloc] init];
    self.line.backgroundColor = [UIColor colorWithRed:0.06 green:0.52 blue:0.98 alpha:1];
    [self.view addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.segment);
        make.top.mas_equalTo(self.segment.mas_bottom);
        make.right.mas_equalTo(self.segment.mas_centerX);
        make.height.mas_equalTo(@2);
    }];
    
    
    //滑动视图..偏移量关联设置
    [self makeScroll];
    
}

- (void)makeScroll{
    self.scroll = [[UIScrollView alloc] init];
    self.scroll.pagingEnabled = YES;
    self.scroll.delegate = self;
    [self.view addSubview:self.scroll];
    [self.scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.line.mas_bottom);
        make.left.right.bottom.mas_equalTo(@0);
    }];
    
    
    //容器视图控制器
    self.newestTC = [[WWYNewestTableViewController alloc] initWithStyle:UITableViewStylePlain];
    self.hotestTC = [[WWYNewestTableViewController alloc] initWithStyle:UITableViewStylePlain];
    
    [self addChildViewController:self.newestTC];
    [self addChildViewController:self.hotestTC];
    
    //添加到scrollView
    [self.scroll addSubview:self.childViewControllers[0].view];
    [self.scroll addSubview:self.childViewControllers[1].view];
    [self.newestTC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(@0);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(SCREEN_HEIGHT - 106);
    }];
    
    [self.hotestTC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(@0);
        make.left.mas_equalTo(self.newestTC.view.mas_right);
        make.size.mas_equalTo(self.newestTC.view);
    }];
    
    //self.edgesForExtendedLayout = UIRectEdgeNone;
    
}

- (void)setModel:(WWYReadListModel *)model{
    if (_model != model) {
        _model = model;
    }
//    //来个菊花
//    self.hub = [[MBProgressHUD alloc] initWithView:self.view];
//    [self.hub show:YES];
//    
#pragma mark 这俩个数据请求应该放在talbeViewcontroller里..
    //初始化数组
    self.newestArray = [NSMutableArray array];
    NSLog(@"数组地址%p", self.newestArray);
    //最新数据请求
    //sort=addtime&start=10&client=2&typeid=27&limit=10
    NSString *type = [NSString stringWithFormat:@"%ld",self.model.type];
    NSDictionary *dic = @{@"sort":@"addtime", @"start":@"client", @"client":@"2", @"typeid":type, @"limit":@"10"};
    
    [LDJ_AppTools postUrl:@"http://api2.pianke.me/read/columns_detail" body:dic result:LDJJSON requestStyle:LDJRequestJSON headerFile:@{@"PHPSESSID":@"t9edu6vnfhio0v638nj9onrgm6"} success:^(id result) {
        for (NSMutableDictionary *list in [[result valueForKey:@"data"] valueForKey:@"list"]) {
            WWYReadDetailModel *model = [WWYReadDetailModel initWithDictionary:list];
            [self.newestArray addObject:model];
        }
//        NSLog(@"array1%@", self.newestArray);
//        //get.set方法都没有
//        NSLog(@"属性地址%p", self.newestTC.array);
//
        self.newestTC.array = self.newestArray;
        
        [self.hub hide:YES];
    } failure:^(NSError *error) {
        NSLog(@"呵呵%@", error);
    }];
    
    
    self.hotestArray = [NSMutableArray array];
    NSDictionary *hotDic = @{@"sort":@"hot", @"start":@"client", @"client":@"2", @"typeid":type, @"limit":@"10"};
    [LDJ_AppTools postUrl:@"http://api2.pianke.me/read/columns_detail" body:hotDic result:LDJJSON requestStyle:LDJRequestJSON headerFile:@{@"PHPSESSID":@"t9edu6vnfhio0v638nj9onrgm6"} success:^(id result) {
        
        for (NSMutableDictionary *list in [[result valueForKey:@"data"] valueForKey:@"list"]) {
            WWYReadDetailModel *model = [WWYReadDetailModel initWithDictionary:list];
            [self.hotestArray addObject:model];
        }
            self.hotestTC.array = self.hotestArray;

         [self.hub hide:YES];
    } failure:^(NSError *error) {
        
        
    }];
}

#pragma mark -下划线, scrollView联动
- (void)changeSubView:(UISegmentedControl *)segment{
    [self.scroll setContentOffset:CGPointMake(segment.selectedSegmentIndex * SCREEN_WIDTH, 0) animated:YES];
    //下划线跟随
    [UIView animateWithDuration:0.3 animations:^{
        self.line.frame = CGRectMake(self.line.frame.size.width * self.segment.selectedSegmentIndex, self.line.frame.origin.y, self.line.frame.size.width, self.line.frame.size.height);
    }];

}

//连动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.segment.selectedSegmentIndex =  floorf(scrollView.contentOffset.x / SCREEN_WIDTH);
    
    //下划线跟随
    [UIView animateWithDuration:0.3 animations:^{
        self.line.frame = CGRectMake(self.line.frame.size.width * self.segment.selectedSegmentIndex, self.line.frame.origin.y, self.line.frame.size.width, self.line.frame.size.height);
    }];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat x =  scrollView.contentOffset.x / 2;
    [UIView animateWithDuration:0 animations:^{
        self.line.frame = CGRectMake(x, self.line.frame.origin.y, self.line.frame.size.width, self.line.frame.size.height);

    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
